<script lang="ts" setup>
import { ref, reactive, watch } from "vue"
import { CirclePlus, RefreshRight, Search, Refresh } from "@element-plus/icons-vue"
// import { CirclePlus, RefreshRight, UploadFilled } from "@element-plus/icons-vue"
import { usePagination } from "@/hooks/usePagination"
// import { formatDateTime } from "@/utils"

import { ElMessage, type FormInstance, type FormRules, ElMessageBox } from "element-plus"
// const fileList = ref<any>([])
import { addInterFaceSet, seeInterFaceSet, editInterFaceSet, delInterFaceSet } from "@/api/msg"
defineOptions({
  name: "MsgInterFaceSet"
})
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()
const loading = ref<boolean>(false)
//#region 增
const dialogVisible = ref<boolean>(false)
const formData = reactive<any>({
  name: "",
  userName: "",
  passWord: ""
})
const formRules: FormRules = reactive({
  name: [{ required: true, trigger: "blur", message: "请输入名称" }],
  userName: [{ required: true, trigger: "blur", message: "请输入账号" }],
  passWord: [{ required: true, trigger: "blur", message: "请输入密码" }]
})

const handleCreate = () => {
  formRef.value?.validate((valid: boolean) => {
    if (valid) {
      if (currentUpdateId.value === undefined) {
        addInterFaceSet(formData).then(() => {
          ElMessage.success("新增成功")
          dialogVisible.value = false
          getTableData()
        })
      } else {
        editInterFaceSet(formData).then(() => {
          ElMessage.success("修改成功")
          console.log("修改")
          dialogVisible.value = false
          getTableData()
        })
      }
    } else {
      return false
    }
  })
}
//#endregion

//#region 删
const handleDelete = (row: any) => {
  ElMessageBox.confirm(`确认删除此项配置？`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(() => {
    delInterFaceSet(row.id).then(() => {
      // ElMessage.success("删除成功")
      getTableData()
    })
  })
}
//#endregion

//#region 改
const currentUpdateId = ref<undefined | string>(undefined)
const handleUpdate = (row: any) => {
  dialogVisible.value = true
  currentUpdateId.value = row.id
  formData.id = row.id
  formData.name = row.name
  formData.userName = row.userName
  formData.passWord = row.passWord
}
//#endregion

// #region 搜索
const searchFormRef = ref<FormInstance | null>(null)
const searchData = reactive({
  name: ""
})
// 查询按键
const handleSearch = () => {
  if (paginationData.currentPage === 1) {
    getTableData()
  }
  paginationData.currentPage = 1
}

// 重置按键
const resetSearch = () => {
  searchFormRef.value?.resetFields()
  if (paginationData.currentPage === 1) {
    getTableData()
  }
  paginationData.currentPage = 1
}

//#region 查
const tableData = ref<any>([])
const getTableData = () => {
  loading.value = true
  seeInterFaceSet({
    page: paginationData.currentPage,
    size: paginationData.pageSize
  })
    .then((res: any) => {
      paginationData.total = res.data.total
      tableData.value = res.data.list
      console.log(res, "查询的数据")
    })
    .catch(() => {
      tableData.value = []
    })
    .finally(() => {
      loading.value = false
    })
}

// 刷新数据
const handleRefresh = () => {
  getTableData()
}
/** 监听分页参数的变化 */
watch([() => paginationData.currentPage, () => paginationData.pageSize], getTableData, { immediate: true })
//#endregion

// #region 清除验证  清除规则报错信息
const formRef = ref<FormInstance | null>(null)
const clearValidateA = () => {
  formRef.value?.clearValidate()
  formRef.value?.resetFields()
}
const resetForm = () => {
  currentUpdateId.value = undefined
  clearValidateA()
  // 清空reactive对象里面的key值
  Object.keys(formData).map((key) => {
    delete formData[key]
  })
}
</script>

<template>
  <div class="app-container">
    <el-card v-loading="loading" shadow="never" class="search-wrapper" style="display: none">
      <el-form ref="searchFormRef" :inline="true" :model="searchData">
        <el-form-item prop="name" label="名称">
          <el-input v-model.trim="searchData.name" placeholder="请输入" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card v-loading="loading" shadow="never">
      <div class="toolbar-wrapper">
        <div>
          <el-button type="primary" :icon="CirclePlus" @click="dialogVisible = true">添加助通账号</el-button>
        </div>
        <div>
          <el-tooltip content="刷新表格">
            <el-button type="primary" :icon="RefreshRight" circle @click="handleRefresh" />
          </el-tooltip>
        </div>
      </div>
      <div class="table-wrapper">
        <el-table :data="tableData">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column prop="name" label="名称" align="center" />
          <el-table-column prop="userName" label="账号" align="center" />
          <el-table-column prop="passWord" label="密码" align="center" />
          <el-table-column fixed="right" label="操作" width="150" align="center">
            <template #default="scope">
              <el-button type="primary" text bg size="small" @click="handleUpdate(scope.row)">修改</el-button>
              <el-button type="danger" text bg size="small" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pager-wrapper">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :total="paginationData.total"
          :page-size="paginationData.pageSize"
          :currentPage="paginationData.currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <!-- 新增 修改 -->
    <el-dialog class="inputdialog" v-model="dialogVisible" title="新增" @close="resetForm" width="60%">
      <el-form ref="formRef" :model="formData" :rules="formRules" label-width="100px" label-position="left">
        <el-form-item prop="name" label="名称">
          <el-input v-model="formData.name" placeholder="请输入" maxlength="20" show-word-limit />
        </el-form-item>
        <el-form-item prop="userName" label="账号">
          <el-input v-model="formData.userName" placeholder="请输入" maxlength="50" show-word-limit />
        </el-form-item>
        <el-form-item prop="passWord" label="密码">
          <el-input v-model="formData.passWord" placeholder="请输入" maxlength="50" show-word-limit />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleCreate">提交</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}
.Signature-matter {
  margin-bottom: 20px;
  border: 1px solid #6cf;
  background: #e5f0ff;
  padding: 10px 14px;
  border-radius: 5px;
  font-size: 12px;
}
:deep(.el-radio-group) {
  display: block;
}
:deep(.el-radio.el-radio--large) {
  display: block !important;
}
</style>
